/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#include "statics.h"
#include "../common.h"
#include "node/app/application.h"
#include "node/net/http/http_request.h"
#include "node/net/http/http_response.h"
#include <csignal>
using Log = ::node::util::Log;
using HttpRequest = ::node::http::HttpRequest;
using HttpResponse = ::node::http::HttpResponse;


StaticsSystem::StaticsSystem(const char *name)
    : SubSystem(name)
    , http_("./")
    , worker_(&http_)
    , log_()
{
    http_.set_user_data(this);
    http_.set_request_callback(&StaticsSystem::OnRequest);
}

StaticsSystem::~StaticsSystem(void)
{

}

bool StaticsSystem::Initialize(void)
{
    log_.Setup(Log::CON_NUL, Log::ROT_FILE_DATE | Log::ROT_FILE_SIZE, "log/", "realmd.statics");
    log_.set_severity_level(Log::SL_DEBUG);

    const char *host = GCONF->GetString("STATICS", "ip", "127.0.0.1");
    int port = GCONF->GetInt("STATICS", "port", 9999);
    return http_.Start(host, port);
}

void StaticsSystem::Run(void)
{
    if (!worker_.Start()) {
        log_.error("[StaticsSystem::Run] Start http worker failed");
    }
    else {
        log_.info("[StaticsSystem::Run] Start http worker ok");
    }
}

void StaticsSystem::Uninitialize(void)
{
    worker_.Join();
}

void StaticsSystem::OnSignal(int signal)
{
    if (signal == SIGINT) {
        http_.Stop();
    }
}

void StaticsSystem::OnRequest(HttpRequest *req, HttpResponse *resp, void *arg)
{
    //StaticsSystem *statics_sys = static_cast<StaticsSystem*>(arg);
    //assert(arg);

    time_t uptime = ::node::app::Application::instance()->uptime();
    UISubSystem::Statics statics = UISYS->GetStatics();

    resp->WritePrintf("<html>\n");
    resp->WritePrintf("<head>\n");
    resp->WritePrintf("<title>%s</title>\n", "[node-wow] realmd statics");
    resp->WritePrintf("</head>\n");
    resp->WritePrintf("<body>\n");
    resp->WritePrintf("<table border=\"1\">\n");
    resp->WritePrintf("<tr>\n");
    resp->WritePrintf("<td>%s</td>", "UPTIME");
    resp->WritePrintf("<td>%s</td>", "TOTAL CONNECTIONS");
    resp->WritePrintf("<td>%s</td>", "AUTHED CONNECTIONS");
    resp->WritePrintf("</tr>\n");
    resp->WritePrintf("<tr>\n");
    resp->WritePrintf("<td>%d</td>", uptime);
    resp->WritePrintf("<td>%d</td>", statics.total_connections);
    resp->WritePrintf("<td>%d</td>", statics.authed_connections);
    resp->WritePrintf("</tr>\n");
    resp->WritePrintf("</table>\n");
    resp->WritePrintf("</body>\n");
    resp->WritePrintf("</html>\n");
    resp->End(200, "OK");
}